package com.softaria.spkiller.layers.classificator.impl;

import java.io.IOException;


import com.softaria.spkiller.metadata.ClassMetaData;

public abstract class AbstractTermPredicate implements ClassificationPredicate{

	public static enum TermMode {
		equals,startsWith,endsWith,contains
	}
	
	private String term;
	private TermMode termMode;
	
	

	public AbstractTermPredicate(String term, TermMode termMode) {
		super();
		this.term = term;
		this.termMode = termMode;
	}



	@Override
	public boolean matches(ClassMetaData metaData, ClassProbe classProbe)
			throws IOException {
		
		String value = getValue(metaData,classProbe);
		
		if((value == null)||(term==null)) {
			return false;
		}
				
		switch(termMode) {
			case contains:
				return value.indexOf(term)!=-1;
			case equals:
				return value.equals(term);
			case endsWith:
				return value.endsWith(term);
			case startsWith:
				return value.startsWith(term);
			
		}
		
		return false;
			
	}



	protected abstract String getValue(ClassMetaData metaData, ClassProbe classProbe);

}
